package co.fitstart.coach.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;

import co.fitstart.entity.coach.CoachRestType;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

@Repository("scheduleDaoSupport")
public class ScheduleDaoSupport {
    
    @Dynamic
    private CourseDao courseDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /**
     * 
     * @param coach
     * @param site
     * @param date
     * @return
     */
    public List<String> listAvaliable(Principal coach, Site site, Date date) {
        
    	List<String> businessTimes = site.getBusinessTimes();
    	List<String> filterTimes = new ArrayList<String>();
    	
    	// add all course_schedule hours into filterTimes
    	List<String> scheduleHours = this.listCourseScheduleHour(coach, date);
    	filterTimes.addAll(scheduleHours);
    	
    	// add business, rest, experience into filter
		List<String> calendarHours = courseDao.listCoachRestHour(coach, date, new CoachRestType[]{CoachRestType.BUSINESS, CoachRestType.REST, CoachRestType.EXPERIENCE});
		filterTimes.addAll(calendarHours);
    	
        businessTimes.removeAll(filterTimes);
        return businessTimes;
    }
    
    /**
     * 
     * @param coach
     * @param date
     * @return
     */
    private List<String> listCourseScheduleHour(Principal coach, Date date) {
        
        List<String> courseHours = courseDao.listCourseHours(coach, date);
        
        List<String> scheduleHours = new ArrayList<String>();
        for(String courseHour : courseHours) {
            String[] arr = courseHour.split(":");
            if(arr[1].equalsIgnoreCase("00")) {
                scheduleHours.add(arr[0] + ":30");
            } else {
                int hour = Integer.parseInt(arr[0]) + 1;
                scheduleHours.add(Integer.valueOf(hour) + ":00");
            }
        }
        
        courseHours.addAll(scheduleHours);
        return courseHours;
    }
    
    /**
     * load CourseSchedule by id
     * 
     * @param id
     * @return
     */
    public CourseSchedule load(Long id) {
        return genericDaoSupport.load(CourseSchedule.class, id);
    }
    
}
